using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    #region Enum: Primary Key Values
    public enum EDiscovery
    {
         Manual = 1,
         Auto = 2,
         System = 3
    }
    #endregion

    //Table-Mapping Class (Customisable half)
    public partial class CDiscovery
    {
        #region Constants
        #endregion

        #region Constructors (Public)
        //Default Connection String
        public CDiscovery() : base() {}

        //Alternative Connection String
        public CDiscovery(CDataSrc dataSrc) : base(dataSrc) {}
        
        //Hidden  (UI code should use cache instead)
        protected internal CDiscovery(byte discoveryId) : base(discoveryId) {}
        protected internal CDiscovery(CDataSrc dataSrc, byte discoveryId) : base(dataSrc, discoveryId) {}
        protected internal CDiscovery(CDataSrc dataSrc, byte discoveryId, IDbTransaction txOrNull) : base(dataSrc, discoveryId, txOrNull) { }
        #endregion

        #region Default Values
        protected override void InitValues_Custom()
        {
            //_sampleDateCreated = DateTime.Now;
            //_sampleSortOrder   = 0;
        }
        #endregion
        
        #region Default Connection String
        protected override CDataSrc DefaultDataSrc()  { return CDataSrc.Default;  }
        #endregion

        #region Properties - Relationships
        //Relationships - Foriegn Keys (e.g parent)

        //Relationships - Collections (e.g. children)
        public    CTypeList Types                     {  get { return CType.Cache.GetByDiscoveryId(          this.DiscoveryId); }  }
        protected CTypeList Types_(IDbTransaction tx) {        return new CType(DataSrc).SelectByDiscoveryId(this.DiscoveryId, tx); } //Only used for cascade deletes

        public CSignatureList Signatures { get { return CSignature.Cache.GetByDiscoveryId(this.DiscoveryId); } }
        protected CSignatureList Signatures_(IDbTransaction tx) { return new CSignature(DataSrc).SelectByDiscoveryId(this.DiscoveryId, tx); } //Only used for cascade deletes

        #endregion

        #region Properties - Customisation
        //Derived/ReadOnly (e.g. xml classes, presentation logic)
        #endregion

        #region Save/Delete Overrides
        //Can Override base.Save/Delete (e.g. Cascade deletes, or insert related records)
        #endregion

        #region Custom Database Queries
        //(Not normally required for cached classes, use list class for searching etc)
        //For Stored Procs can use: MakeList (matching schema), or DataSrc.ExecuteDataset (reports etc)
        //For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
        #endregion

        #region Searching (Optional)
        //For cached classes, custom seach logic resides in static methods on the list class
        // e.g. CDiscovery.Cache.Search("...")

        //See also the auto-generated methods based on indexes
        //' e.g. CDiscovery.Cache.GetBy...
        #endregion

        #region Caching Details
        //Cache Key
        internal static string CACHE_KEY = typeof(CDiscovery).ToString();    //TABLE_NAME

        //Cache data
        private static CDiscoveryList LoadCache()
        {
            var v = new CDiscovery().SelectAll();
            bool reload = false;
            foreach (int id in Enum.GetValues(typeof(EDiscovery)))
            {
                var e = (EDiscovery)id;
                var d = v.GetById((byte)id);
                if (null == d)
                {
                    var nv = new CNameValueList("DiscoveryId", id, "DiscoveryName", e.ToString());
                    CDataSrc.Default.Insert(TABLE_NAME, PRIMARY_KEY_NAME, true, nv, null, null);
                    reload = true;
                }
            }
            if (reload)
                v = new CDiscovery().SelectAll();
            return v;
        }

        //Cache Timeout
        private static void SetCache(CDiscoveryList value)
        {
            if (null != value)  
                value.Sort(); 
            CCache.Set(CACHE_KEY, value);    //Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
        }
        //Helper Method
        private CDiscovery CacheGetById(CDiscoveryList list)  { return list.GetById(this.DiscoveryId);    }
        #endregion

        #region Cloning
        public CDiscovery Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            //Shallow copy: Copies the immediate record, excluding autogenerated Pks
            CDiscovery copy = new CDiscovery(this, target);

            //Deep Copy - Child Entities: Cloned children must reference their cloned parent
            //copy.SampleParentId = parentId;

            copy.Save(txOrNull);

            //Deep Copy - Parent Entities: Cloned parents also clone their child collections
            //this.Children.Clone(target, txOrNull, copy.DiscoveryId);

            return copy;
        }
        #endregion

        #region ToXml
        protected override void ToXml_Custom(System.Xml.XmlWriter w)
        {
            //Store(w, "Example", this.Example)
        }
        #endregion
    }
}